package com.ruoyi.entwechat.util;

import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;

/**
 * 企业微信URL
 *
 * @author xueyu
 */
@Slf4j
public class ApiUrl {

    private static final String ENT_BASE_ADDR = "https://qyapi.weixin.qq.com";


    private static String getEntBaseAddr() {
        return ENT_BASE_ADDR;
    }

    /**
     * 获取token的url。
     *
     * @param corpId 企业id
     * @param secret 密钥
     * @return str
     */
    public static String getTokenUrl(String corpId, String secret) {
        String entWeChatAddr = getEntBaseAddr();
        return entWeChatAddr + "/cgi-bin/gettoken?corpid=" + corpId + "&corpsecret=" + secret;
    }

    /**
     * 获取授权用户信息。
     *
     * @param token 令牌
     * @param code  前台授权码
     * @return string
     */
    public static String getAuthGetUserInfoUrl(String token, String code) {
        Assert.notBlank(token);
        Assert.notBlank(code);
        String entWeChatAddr = getEntBaseAddr();
        return entWeChatAddr + "/cgi-bin/user/getuserinfo?access_token=" + token + "&code=" + code;
    }

    /**
     * 发送消息
     *
     * @param accessToken 接口令牌
     * @return string
     */
    public static String getMessageSendUrl(String accessToken) {
        String entWeChatAddr = getEntBaseAddr();
        return entWeChatAddr + "/cgi-bin/message/send?access_token=" + accessToken;
    }

    /**
     * 读取成员信息
     *
     * @param accessToken 令牌
     * @param userId      用户id
     * @return String
     */
    public static String getAddressBookUserInfoUrl(String accessToken, String userId) {
        String entWeChatAddr = getEntBaseAddr();
        return entWeChatAddr + "/cgi-bin/user/get?access_token=" + accessToken + "&userid=" + userId;
    }


    public static String getUserDetailUrl(String accessToken) {
        String entWeChatAddr = getEntBaseAddr();
        return entWeChatAddr + "/cgi-bin/auth/getuserdetail?access_token=" + accessToken;
    }

    public static String getUserIdByPhone(String accessToken) {
        return getEntBaseAddr() + "/cgi-bin/user/getuserid?access_token=" + accessToken;
    }

    public static String getJsapiTicketUrl(String accessToken) {
        String entWeChatAddr = getEntBaseAddr();
        return entWeChatAddr + "/cgi-bin/get_jsapi_ticket?access_token=" + accessToken;
    }

    public static String getUpdateTemplateCardUrl(String accessToken) {
        return getEntBaseAddr() + "/cgi-bin/message/update_template_card?access_token=" + accessToken;
    }
}
